package com.frejoys.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.UserNameAndPhoneDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.dto.common.user.AvatarUpDto;
import com.frejoys.common.dto.common.user.InfoUpDto;
import com.frejoys.common.dto.common.user.NicknameUpDto;
import com.frejoys.common.dto.common.user.PasswordUpDto;
import com.frejoys.common.enums.ESite;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.AppConfigUtil;
import com.frejoys.common.util.EncryptUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.UserAgentVo;
import com.frejoys.common.vo.common.AvatarVo;
import com.frejoys.common.vo.common.TokenVo;
import com.frejoys.common.vo.common.UserDetailsVo;
import com.frejoys.common.vo.common.UserVo;
import com.frejoys.dao.util.UserVoUtil;
import com.frejoys.service.CommonService;
import com.frejoys.service.UserCodeService;
import com.frejoys.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "用户", description = "用户")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixApp)
@Authorization(Authority.Private)
public class UserController {

    private final UserService userService;

    private final CommonService commonService;

    private final RedissonClient redissonClient;

    private final UserCodeService userCodeService;


    @Operation(summary = "修改昵称")
    @PostMapping("/user/upNickname")
    public Result upNickname(@RequestBody @Validated NicknameUpDto dto) {
        userService.upNickname(RequestUtil.userId(), dto.getNickname());
        return Result.success();
    }

    @Operation(summary = "修改头像")
    @PostMapping("/user/upAvatar")
    public Result upAvatar(@RequestBody @Validated AvatarUpDto dto) {
        userService.upAvatar(RequestUtil.userId(), dto.getAvatar());
        return Result.success();
    }

    @Operation(summary = "修改信息")
    @PostMapping("/user/upInfo")
    public Result upInfo(@RequestBody @Validated InfoUpDto dto) {
        userService.upInfo(RequestUtil.userId(), dto);
        return Result.success();
    }

    @Operation(summary = "获取头像")
    @PostMapping("/user/getAvatar")
    public Result<AvatarVo> getAvatar() {
        AvatarVo vo = new AvatarVo();
        String avatar = userService.getAvatar(RequestUtil.userId());
        vo.setAvatar(AppConfigUtil.getUrlPrefix(avatar));
        return Result.success(vo);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/user/upPassword")
    public Result upPassword(@RequestBody @Validated PasswordUpDto dto) {
        userService.upPassword(RequestUtil.userId(), EncryptUtil.appAes.decryptStr(dto.getPassword()));
        return Result.success();
    }

    @Operation(summary = "信息")
    @PostMapping("/user/getInfo")
    public Result<UserVo> getInfo() {
        return Result.success(UserVoUtil.toVo(userService.getInfo(RequestUtil.userId())));
    }

    @Operation(summary = "退出登录")
    @PostMapping("/user/logout")
    public Result logout() {
        userService.logout(RequestUtil.userId(), ESite.app.getValue(), RequestUtil.siteType());
        return Result.success();
    }

    @Operation(summary = "注销账户")
    @PostMapping("/user/logoff")
    public Result logoff() {
        userService.delInfo(RequestUtil.userId());
        return Result.success();
    }

    @Operation(summary = "获取用户信息")
    @PostMapping("/user/getUserInfo")
    public Result<UserDetailsVo> getUserInfo() {
        return Result.success(userService.getUserInfo(RequestUtil.userId(), ESite.app.getValue()));
    }


    /**
     * 登录刷新token
     */
    @Operation(summary = "登录刷新token")
    @PostMapping("/user/refreshToken")
    Result<TokenVo> userRefreshToken() {
        return Result.success(userService.refreshToken(RequestUtil.userId(), ESite.app.getValue(), RequestUtil.siteType()));
    }

    /**
     * 验证者身份
     */
    // @Operation(summary = "验证推广者身份")
    // @PostMapping("/user/verifyIdentify")
    // Result<UserExtendVerifyVo> verifyIdentify(@RequestBody @Validated UserCodeDto dto) {
    //
    //     Long id = commonService.codeByUserId(dto.getCode());
    //     return Result.success(commonService.verifyIdentify(userService.getInfo(id), userService.getSimpleUser(RequestUtil.userId())));
    // }

    /**
     * 推广
     */
    // @Operation(summary = "推广用户成为达人或代理")
    // @PostMapping("/user/userExtend")
    // public Result userExtend(@RequestBody @Validated UserCodeDto dto) {
    //
    //     RLock lock = redissonClient.getLock(CacheKeyUtil.userExtendLockKey(RequestUtil.userId()));
    //
    //     if (lock.tryLock()) {
    //         try {
    //             Long id = commonService.codeByUserId(dto.getCode());
    //             commonService.userExtend(userService.getInfo(id), RequestUtil.userId());
    //
    //         } finally {
    //             if (lock.isLocked() && lock.isHeldByCurrentThread()) {
    //                 lock.unlock();
    //             }
    //         }
    //     } else {
    //         throw new GeneralException(HttpStatus.beingProcessed);
    //     }
    //
    //     userService.clearUser(RequestUtil.userId());
    //
    //     return Result.success();
    // }


    // @PostMapping("/user/verifyAgentUserId")
    // @Operation(summary = "校验代理人编号是否错误")
    // public Result verifyAgentUserId(@RequestBody @Validated AgentUserIdDto dto){
    //     userService.verifyAgentUserId(dto.getAgentUserId());
    //     return Result.success();
    // }

    @PostMapping("/user/agentListInfo")
    @Operation(summary = "查询代理人列表")
    public Result<List<UserAgentVo>> agentList(@RequestBody @Validated UserNameAndPhoneDto dto){
        return Result.success(userService.agentList(dto));
    }
}
